How to Use Printer by Android Programmable
We usually connect the printer to Windows using USB and Wifi, but for some models, the system also needs to install the printer driver. Calling the printer on Android is slightly different.
Before Android 4.4, a third-party solution was needed to implement the printing function, or a protocol for communicating with the printer was implemented. After 4.4, Android introduced the Print class as a printing tool class, such as PrintHelper for printing. Bitmap, PrintPdfDocument to print the document.
Print Resource Bitmap
Here to print Bitmap as an example, first look at the official documentation, you can see the sample code for printing the resource is as follows
1 | private void doPhotoPrint() { |
Among them, setScaleMode
is used to set the mode of printing. It should be noted that many printer service providers do not make this adaptation, so it is likely that the adjustment will not work.
Actual style of print page
The second way is to print the View on the page, here to use the cache to get the elements to be printed, but note that the cache is more expensive, so close the cache as soon as you get the printed View. The sample code is as follows:
1 | private void doPhotoPrint() { |
Adjustable Style After Printing Layout
The third way is the most customized way, you can use the canvas to customize after the Bitmap is generated, and the image sharing function on many APPs involves this method.
First, we first write a function that converts the View into a Bitmap, as follows
1 | private static Bitmap getBitmapFromView(View view) { |
Then similar to the above
1 | private void doPhotoPrint(View v) { |
More customization can be achieved by modifying the canvas.
Attention
After Android 4.4, Android and the printer actually connect through Android system printing, that is, as the application layer, do not care about the connection between the printer and the Android system, only need to evoke the system’s print service.
So how do Android and printers connect? On the hardware level, you can connect via USB or Wifi or RJ45. Printer manufacturers should develop corresponding print service drivers for Android systems, which can be downloaded from Play Store or domestic application markets. Then enable the service in the system settings. It’s important to note that if you are connecting over a network, check that both Android and the printer are successfully assigned to the IP.
But in my option, the best way is using the *nix-like system to be a server which connected to a printer, Android just for the client.